package com.milasoft.queueingcloud;

import java.util.List;


import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ShowQueueHandler extends ActionBarActivity{

	ListView listview;
	ListView listView1;
	List<ParseObject> ob;
	ProgressDialog mProgressDialog;
	ArrayAdapter<String> adapter;
	ArrayAdapter<String> adapter2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from listview_main.xml
		
		setContentView(R.layout.listview_main);
		// Execute RemoteDataTask AsyncTask
		new RemoteDataTask().execute();
	}

	// RemoteDataTask AsyncTask
	private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Create a progressdialog
			mProgressDialog = new ProgressDialog(ShowQueueHandler.this);
			// Set progressdialog title
			mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
			// Set progressdialog message
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);
			// Show progressdialog
			mProgressDialog.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// Locate the class table named "Country" in Parse.com
			ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
					"CustomersQueue");
			query.orderByDescending("_created_at");
			try {
				ob = query.find();
			} catch (ParseException e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// Locate the listview in listview_main.xml
			listview = (ListView) findViewById(R.id.listview);
			listView1 = (ListView) findViewById(R.id.listView1);
			// Pass the results into an ArrayAdapter
			adapter = new ArrayAdapter<String>(ShowQueueHandler.this,
					R.layout.listview_item);
			adapter2 = new ArrayAdapter<String>(ShowQueueHandler.this,
					R.layout.listview_item2);
			// Retrieve object "name" from Parse.com database
			for (ParseObject UserName : ob) {
				adapter.add((String) UserName.get("UserName"));

			}
			for (ParseObject QueueNo : ob) {
				adapter2.add((String) QueueNo.get("QueueNo").toString());

			}
			// Binds the Adapter to the ListView
			listview.setAdapter(adapter);
			listView1.setAdapter(adapter2);
			// Close the progressdialog
			mProgressDialog.dismiss();
			// Capture button clicks on ListView items
			listview.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// Send single item click data to SingleItemView Class
					Intent i = new Intent(ShowQueueHandler.this,
							SingleItemView.class);
					// Pass data "name" followed by the position
					i.putExtra("UserName",
							ob.get(position).getString("UserName").toString());
					// Open SingleItemView.java Activity
					startActivity(i);
				}
			});
		}
	}
	
}
